package com.xiaxiaoge.tutorialmod.block.custom;

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;

/**
 * @author 夏小哥
 * @version 1.0
 */
public class JumpyBlock extends Block {

    public JumpyBlock(Properties properties) {
        super(properties);
    }

    /**
     *  右键该方块触发事件
     */
    @Override
    public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult blockHitResult) {
        // 一共会被触发四次
        // Server 左手 右手
        // Client 左手 右手

        if(!level.isClientSide && hand == InteractionHand.MAIN_HAND) {
            player.sendSystemMessage(Component.literal("Hello Minecraft!"));
        }
        return super.use(blockState, level, blockPos, player, hand, blockHitResult);
    }

    /**
     * 在该方块上方时触发事件
     */
    @Override
    public void stepOn(Level level, BlockPos blockPos, BlockState blockState, Entity entity) {
        super.stepOn(level, blockPos, blockState, entity);
        // 检测该方块上的物体是否是活体
        if(entity instanceof LivingEntity livingEntity) {
            // 增加一个跳跃效果
            livingEntity.addEffect(new MobEffectInstance(MobEffects.JUMP,200));
        }

    }
}
